<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Hello World</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="tutorials.html">Tutorials</a>&nbsp;&gt;&nbsp;<a href="bootstrap.html">Bootstrap</a>&nbsp;&gt;&nbsp;<a href="hello-world.html">Hello World</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Hello World</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=13924">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=13924">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=13924">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=13924">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=13924">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=13924">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><p>When you submit a HTML form to the framework, the input is not sent to another server page, but to a Java class that you provide. These classes are called Actions. After the Action fires, a Result selects a resource to render the response. The resource is generally a server page, but it can also be a PDF file, an Excel spreadsheet, or a Java applet window.</p>

<p>Suppose you want to create a simple "Hello World" example that displays a <a shape="rect" class="external-link" href="http://www.PlanetStruts.org/struts2-blank/example/HelloWorld.action" rel="nofollow">welcome message</a>. After setting up an empty "tutorial" web application (see <a shape="rect" href="ready-set-go.html">Ready, Set, Go!</a>), to create a "Hello World" example, you need to do three things:</p>
<ol><li>Create a server page to present the messages</li><li>Create an Action class to create the message</li><li>Create a mapping to couple the action and page
<div class="confluence-information-macro confluence-information-macro-tip"><span class="aui-icon aui-icon-small aui-iconfont-approve confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>By creating these components, we are separating the workflow into three well-known concerns: the View, the Model, and the Controller. Separating concerns makes it easier to manage applications as they become more complex.</p></div></div>
Let's look at an example Action, server page, and mapping. If you like, fire up your IDE, and enter the code as we go.</li></ol>


<h2 id="HelloWorld-TheCode">The Code</h2>

<p>First, we need a server page to present the message.</p>
<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>HelloWorld.jsp</b></div><div class="codeContent panelContent pdl">
<pre class="brush: html; gutter: false; theme: Default" style="font-size:12px;">
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Hello World!&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h2&gt;&lt;s:property value="message" /&gt;&lt;/h2&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
</div></div>
<p>Second, we need an Action class to create the message.</p>
<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>HelloWorld.java</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {

    public static final String MESSAGE = "Struts is up and running ...";

    public String execute() throws Exception {
        setMessage(MESSAGE);
        return SUCCESS;
    }

    private String message;

    public void setMessage(String message){
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
</pre>
</div></div>
<p>Third, we need a mapping to tie it all together.</p>

<p>Edit the <code>struts.xml</code> file to add the <code>HelloWorld</code> mapping.</p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>struts.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
&lt;struts&gt;
    &lt;package name="tutorial" extends="struts-default"&gt;
        &lt;action name="HelloWorld" class="tutorial.HelloWorld"&gt;
            &lt;result&gt;/HelloWorld.jsp&lt;/result&gt;
        &lt;/action&gt;
        &lt;!-- Add your actions here --&gt;
    &lt;/package&gt;
&lt;/struts&gt;
</pre>
</div></div>
<p>Go ahead and try it now! Deploy the application and open <a shape="rect" class="external-link" href="http://localhost:8080/tutorial/HelloWorld.action" rel="nofollow">http://localhost:8080/tutorial/HelloWorld.action</a> and see what happens! You should see a page with the title "Hello World!" and the message "Struts is up and running!". </p>

<div class="confluence-information-macro confluence-information-macro-warning"><p class="title">Don't forget</p><span class="aui-icon aui-icon-small aui-iconfont-error confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>Compile your Action to <code>WEB-INF/classes</code> and restart your container if necessary. If you are using maven, you can just run:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&gt; mvn jetty:run
</pre>
</div></div></div></div>

<h2 id="HelloWorld-HowtheCodeWorks">How the Code Works</h2>

<p>Your browser sends to the web server a request for the URL <a shape="rect" class="external-link" href="http://localhost:8080/tutorial/HelloWorld.action" rel="nofollow">http://localhost:8080/tutorial/HelloWorld.action</a>.</p>
<ol><li>The container receives from the web server a request for the resource <code>HelloWorld.action</code>. According to the settings loaded from the <a shape="rect" href="webxml.html">web.xml</a>, the container finds that all requests are being routed to <code>org.apache.struts2.dispatcher.FilterDispatcher</code>, including the <code>*.action</code> requests. The FilterDispatcher is the entry point into the framework.</li><li>The framework looks for an action mapping named "HelloWorld", and it finds that this mapping corresponds to the class "HelloWorld". The framework instantiates the Action and calls the Action's <code>execute</code> method.</li><li>The <code>execute</code> method sets the message and returns <code>SUCCESS</code>. The framework checks the action mapping to see what page to load if <code>SUCCESS</code> is returned. The framework tells the container to render as the response to the request, the resource <code>HelloWorld.jsp</code>.</li><li>As the page <code>HelloWorld.jsp</code> is being processed, the <code>&lt;s:property value="message" /&gt;</code> tag calls the getter <code>getMessage</code> of the <code>HelloWorld</code> Action, and the tag merges into the response the value of the message.</li><li>A pure HTML response is sent back to the browser.</li></ol>


<p>For detailed information on Struts 2 architecture see <a shape="rect" href="big-picture.html">Big Picture</a>.</p>

<h2 id="HelloWorld-TestingActions">Testing Actions</h2>

<p>Testing an Action is easy. Here's a test for our Hello World Action.</p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>HelloWorldTest.java</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package tutorial;
import junit.framework.TestCase;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldTest extends TestCase {
  public void testHelloWorld() throws Exception {

    HelloWorld hello_world = new HelloWorld();
    String result = hello_world.execute();

    assertTrue("Expected a success result!",
      ActionSupport.SUCCESS.equals(result));

    assertTrue("Expected the default message!",
      HelloWorld.MESSAGE.equals(hello_world.getMessage()));

    }
}
</pre>
</div></div>

<h2 id="HelloWorld-WhattoRemember">What to Remember</h2>

<p>The framework uses Actions to process HTML forms and other requests. The <code>Action</code> class returns a result-name such as <code>SUCCESS</code>, <code>ERROR</code>, or <code>INPUT</code>. Based on the mappings loaded from the <code>struts.xml</code>, a given result-name may select a page (as in this example), another action, or some other web resource (image, PDF). </p>

<p>When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.</p>

<div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> Next </p></th><td colspan="1" rowspan="1" class="confluenceTd"><p> Onward to <a shape="rect" href="using-tags.html">Using Tags</a> </p></td></tr><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> Prev </p></th><td colspan="1" rowspan="1" class="confluenceTd"><p> Return to <a shape="rect" href="ready-set-go.html">Ready, Set, Go!</a> </p></td></tr></tbody></table></div></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
